<!DOCTYPE html>
<html lang="en-US">
    <head>
        <title>Switch::Plain - a simple switch statement for Perl - metacpan.org</title>
        <link rel="preload" as="fetch" href="https://metacpan.org/account/login_status" crossorigin="anonymous" />
        <link href="https://metacpan.org/_assets/b8ccceeed47a0652049703d99326a9cea4933443.css" rel="stylesheet" type="text/css">
        <script src="https://metacpan.org/_assets/6bfedafe2d7caa915b7d84f61b45936818e3242e.js" type="text/javascript" defer></script>
        <link rel="alternate" type="application/rss+xml" title="Recent CPAN Uploads of Switch-Plain - MetaCPAN" href="https://metacpan.org/dist/Switch-Plain/releases.rss" />
        <link rel="canonical" href="./Switch::Plain.html" />
        <meta name="description" content="a simple switch statement for Perl" />
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=5">
        <link rel="shortcut icon" href="https://metacpan.org/static/icons/favicon.ico">
        <link rel="apple-touch-icon" sizes="152x152" href="https://metacpan.org/static/icons/apple-touch-icon.png">
        <link rel="search" href="https://metacpan.org/static/opensearch.xml" type="application/opensearchdescription+xml" title="MetaCPAN">
        <script>
          (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
          (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
          })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

          (function(skey, ga_id){
            ga('create', ga_id, {
              siteSpeedSampleRate : 100,
              storage             : 'none',
              clientId            : localStorage.getItem(skey)
            });
            ga(function(tracker) {
              localStorage.setItem(skey, tracker.get('clientId'));
            });
            ga('send', 'pageview');
          })('ga:clientId', 'UA-27829474-1');
        </script>
<meta name="twitter:card"        content="summary" />
<meta name="twitter:url"         content="https://metacpan.org/pod/Switch::Plain" />
<meta name="twitter:title"       content="Switch::Plain" />
<meta name="twitter:description" content="a simple switch statement for Perl" />
<meta name="twitter:site"        content="metacpan" />
    </head>
    <body>
        <nav class="navbar navbar-default" role="navigation">
            <div class="header-logo-large hidden-xs">
              <a href="https://metacpan.org/" tabindex="0">
                <svg class="logo" aria-label="MetaCPAN">
                  <use class="logo" href="/static/images/metacpan-logo.svg#logo" />
                </svg>
              </a>
            </div>
            <div class="header-logo-icon visible-xs">
              <a href="https://metacpan.org/">
                <svg class="logo" aria-label="MetaCPAN">
                  <use class="logo" href="/static/images/metacpan-logo.svg#dots" />
                </svg>
              </a>
            </div>
            <ul class="nav navbar-nav menu-items hidden-xs hidden-sm">
              <li><a href="https://metacpan.org/about">About</a></li>
              <li><a href="https://metacpan.org/about/sponsors">Sponsor</a></li>
              <li><a href="https://grep.metacpan.org/">grep::cpan</a></li>
              <li><a href="https://metacpan.org/recent">Recent</a></li>
              <li><a href="https://metacpan.org/about/faq">FAQ</a></li>
              <li><a href="https://metacpan.org/tools">Tools</a></li>
              <li><a href="https://fastapi.metacpan.org/">API</a></li>
            </ul>
            <ul class="nav navbar-nav navbar-right">
                <button type="button" class="searchbar-btn visible-xs visible-sm">
                    <i class="fa fa-search button-fa-icon"></i>
                </button>
                <form action="https://metacpan.org/search" class="searchbar-form visible-md visible-lg search-form form-horizontal">
                   <input type="hidden" name="size" id="metacpan_search-size" value="20">
                  <div class="form-group">
                      <div class="search-group">
                        <i class="fa fa-search"></i>
                        <input type="text" name="q" placeholder="Search the CPAN" size="41" autocorrect="off" autocapitalize="off" spellcheck="false" id="metacpan_search-input" class="form-control" value="">
                      </div>
                  </div>
                </form>
                    <li class="icon-slidepanel visible-xs visible-sm">
                      <button data-toggle="slidepanel" data-target=".slidepanel">
                        <span class="button-fa-icon">
                          <i class="fa fa-bars slidepanel-open"></i>
                          <i class="fa fa-times slidepanel-close"></i>
                        </span>
                      </button>
                    </li>
                <form action="https://metacpan.org/account/logout" method="POST" id="metacpan-logout"></form>
                <li class="dropdown logged_in" style="display: none;">
                    <button type="button" class="dropdown-toggle" data-toggle="dropdown">
                      <i class="fa fa-user button-fa-icon logged-in-icon" aria-hidden="true"></i>
                      <i class="fas fa-chevron-down"></i>
                    </button>
                    <ul class="dropdown-menu">
                        <li><a href="https://metacpan.org/account/identities">Identities</a></li>
                        <li><a href="https://metacpan.org/account/profile">Profile</a></li>
                        <li><a href="https://metacpan.org/account/favorite/list">Favorites</a></li>
                        <li>
                            <a href="./Switch::Plain.html#" type="button" onclick="$('#metacpan-logout').submit(); return false">
                              Logout
                            </a>
                        </li>
                    </ul>
                </li>
                <li class="dropdown logged_out" style="display: none;">
                    <button type="button" class="dropdown-toggle" data-toggle="dropdown">
                      <i class="fa fa-user button-fa-icon" aria-hidden="true"></i>
                      <i class="fas fa-chevron-down"></i>
                    </button>
                    <ul class="dropdown-menu">
                        <li>
                            <a href="https://metacpan.org/login/github">
                                <i class="fab fa-github fa-fw"></i>
                                GitHub
                            </a>
                        </li>
                        <li>
                            <a href="https://metacpan.org/login/twitter">
                                <i class="fab fa-twitter fa-fw"></i>
                                Twitter
                            </a>
                        </li>
                        <li>
                            <a href="https://metacpan.org/login/google">
                                <i class="fab fa-google fa-fw"></i>
                                Google
                            </a>
                        </li>
                    </ul>
                </li>
                <li class="dropdown logged_placeholder">
                    <button>
                      <i class="fa fa-user button-fa-icon" aria-hidden="true"></i>
                    </button>
                </li>
            </ul>
        </nav>
        <div class="page-content ">
          <!--
          <div class="top-notify-banner">
            <i class="fas fa-info-circle"></i>
          </div>
          -->
          <nav class="sidebar">
            <div class="slidepanel">
              <ul class="nav-list ">
    <li class="nav-header no-margin-top">
      <div class="ttip" data-toggle="tooltip" data-placement="bottom" title="The date that this version of Switch-Plain was released.">
      <span class="relatize">16 Apr 2017 20:53:43 UTC</span>
    </li>
  <li>
    Distribution: <a href="https://metacpan.org/dist/Switch-Plain">Switch-Plain</a>
  </li>
  <li>
    Module version: 0.0501
  </li>
  <li>
    <a data-keyboard-shortcut="g s" href="https://metacpan.org/dist/Switch-Plain/source/lib/Switch/Plain.pm">Source</a>
    (<a href="https://metacpan.org/dist/Switch-Plain/source/lib/Switch/Plain.pm?raw=1">raw</a>)
  </li>
  <li>
    <a data-keyboard-shortcut="g b" href="https://metacpan.org/dist/Switch-Plain/source/lib/Switch">Browse</a>
    (<a href="https://metacpan.org/dist/Switch-Plain/source/lib/Switch?raw=1">raw</a>)
  </li>
    <li>
      <a data-keyboard-shortcut="g c" href="https://metacpan.org/dist/Switch-Plain/changes">Changes</a>
    </li>
    <li>
      <a class="nopopup" href="https://metacpan.org/dist/Switch-Plain/contribute">How to Contribute</a>
    </li>
    <li>
        <a rel="noopener nofollow" data-keyboard-shortcut="g r" href="https://github.com/mauke/Switch-Plain">Repository</a>
    </li>
    <li>
      <a rel="noopener nofollow" data-keyboard-shortcut="g i" href="https://rt.cpan.org/Public/Dist/Display.html?Name=Switch-Plain">Issues</a>
      (0)
    </li>
    <li>
      <a rel="noopener nofollow" href="http://matrix.cpantesters.org/?dist=Switch-Plain+0.0501" title="Matrix">Testers</a>
        <span title="(pass / fail / na)">(<a rel="noopener nofollow" href="https://www.cpantesters.org/distro/S/Switch-Plain.html?oncpan=1&amp;distmat=1&amp;version=0.0501&amp;grade=2" style="color: #090">910</a> / <a rel="noopener nofollow" href="https://www.cpantesters.org/distro/S/Switch-Plain.html?oncpan=1&amp;distmat=1&amp;version=0.0501&amp;grade=3" style="color: #900">0</a> / <a rel="noopener nofollow" href="https://www.cpantesters.org/distro/S/Switch-Plain.html?oncpan=1&amp;distmat=1&amp;version=0.0501&amp;grade=4">80</a>)</span>
    </li>
    <li>
      <a rel="noopener nofollow" href="http://cpants.cpanauthors.org/release/MAUKE/Switch-Plain-0.0501">Kwalitee</a>
    </li>
    <li>
      <div class="ttip" data-toggle="tooltip" data-placement="bottom" title="The # people with an indexing permission on Switch-Plain who have released something to CPAN in the last 2 years (i.e. the # people likely able to release critical fixes in a timely manner)">
      Bus factor: 1
      </div>
    </li>
    <li>
      <a rel="noopener nofollow" href="http://cpancover.com/latest/Switch-Plain-0.0501/index.html">75.61% Coverage </a>
    </li>
    <li>
      License: perl_5
    </li>
    <li>
      Perl: v5.14.0
    </li>
    <li class="nav-header">Activity</li>
    <li>
<div class="activity-graph">
    <img src="https://metacpan.org/dist/Switch-Plain/activity.svg?res=month" />
    <div class="comment">24 month</div>
</div>
    </li>
    <li class="nav-header">Tools</li>
    <li>
      <a itemprop="downloadUrl" href="https://cpan.metacpan.org/authors/id/M/MA/MAUKE/Switch-Plain-0.0501.tar.gz">
      Download (<span itemprop="fileSize">12.64KB</span>)</a>
    </li>
    <li>
      <a href="https://explorer.metacpan.org/?url=%2Fmodule%2FMAUKE%2FSwitch-Plain-0.0501%2Flib%2FSwitch%2FPlain.pm">
        MetaCPAN Explorer
      </a>
    </li>
    <li>
      <a href="https://metacpan.org/dist/Switch-Plain/permissions">
        Permissions
      </a>
    </li>
    <li>
      <a href="https://metacpan.org/dist/Switch-Plain/releases.rss">
        Subscribe to distribution
      </a>
    </li>
    <li>
      <button class="btn btn-link" data-toggle="modal" data-target="#metacpan_install-instructions-dialog">
        Install Instructions
      </button>
    </li>
    <li>
      <form action="https://metacpan.org/search">
        <input type="hidden" name="q" value="dist:Switch-Plain">
        <input type="search" name="q" placeholder="Search distribution" class="form-control tool-bar-form">
        <input type="submit" style="display: none">
      </form>
    </li>
    <li>
      <form action="https://grep.metacpan.org/search">
        <input type="hidden" name="qd" value="Switch-Plain">
        <input type="hidden" name="source" value="metacpan">
        <input type="search" name="q" placeholder="grep distribution" class="form-control tool-bar-form">
        <input type="submit" style="display: none">
     </form>
    </li>
    <li class="version-jump">
<select onchange="document.location.href=&#39;/release/&#39;+this.value+&#39;/view/lib/Switch/Plain.pm&#39;" class="form-control tool-bar-form">
  <option disabled selected>Jump to version</option>
<option
  disabled
  value="MAUKE/Switch-Plain-0.0501"
>0.0501
  (MAUKE on 2017-04-16)</option>
<option
  
  value="MAUKE/Switch-Plain-0.05"
>0.05
  (MAUKE on 2016-06-10)</option>
<optgroup label="BackPAN">'
<option
  
  value="MAUKE/Switch-Plain-0.04"
>0.04
  (MAUKE on 2016-01-15)</option>
<option
  
  value="MAUKE/Switch-Plain-0.03_01"
>0.03_01 DEV
  (MAUKE on 2016-01-13)</option>
<option
  
  value="MAUKE/Switch-Plain-0.03"
>0.03
  (MAUKE on 2014-12-22)</option>
<option
  
  value="MAUKE/Switch-Plain-0.02"
>0.02
  (MAUKE on 2013-09-23)</option>
<option
  
  value="MAUKE/Switch-Plain-0.01"
>0.01
  (MAUKE on 2012-10-18)</option>
</optgroup>
</select>
    </li>
    <li class="version-diff">
<select onchange="document.location.href='/release/MAUKE/Switch-Plain-0.0501/diff/' + encodeURIComponent(this.value) + '/lib/Switch/Plain.pm'
" class="form-control tool-bar-form">
  <option disabled selected>Diff with version</option>
<option
  disabled
  value="MAUKE/Switch-Plain-0.0501"
>0.0501
  (MAUKE on 2017-04-16)</option>
<option
  
  value="MAUKE/Switch-Plain-0.05"
>0.05
  (MAUKE on 2016-06-10)</option>
<optgroup label="BackPAN">'
<option
  
  value="MAUKE/Switch-Plain-0.04"
>0.04
  (MAUKE on 2016-01-15)</option>
<option
  
  value="MAUKE/Switch-Plain-0.03_01"
>0.03_01 DEV
  (MAUKE on 2016-01-13)</option>
<option
  
  value="MAUKE/Switch-Plain-0.03"
>0.03
  (MAUKE on 2014-12-22)</option>
<option
  
  value="MAUKE/Switch-Plain-0.02"
>0.02
  (MAUKE on 2013-09-23)</option>
<option
  
  value="MAUKE/Switch-Plain-0.01"
>0.01
  (MAUKE on 2012-10-18)</option>
</optgroup>
</select>
    </li>

    <li>
<ul class="dependencies">
  <li class="nav-header">Dependencies</li>
  <li><a href="https://metacpan.org/pod/Carp" title="Carp" class="ellipsis">Carp</a></li>
  <li><a href="https://metacpan.org/pod/XSLoader" title="XSLoader" class="ellipsis">XSLoader</a></li>
  <li><a href="https://metacpan.org/pod/warnings" title="warnings" class="ellipsis">warnings</a></li>
  <li>
    <hr>
  </li>
  <li>
    <a href="https://metacpan.org/module/Switch::Plain/requires">Reverse dependencies</a>
  </li>
  <li>
    <a href="http://deps.cpantesters.org/?module=Switch%3A%3APlain">CPAN Testers List</a>
  </li>
  <li>
    <a href="https://cpandeps.grinnz.com/?dist=Switch-Plain">Dependency graph</a>
  </li>
</ul>
    </li>
    <li class="nav-header">Permalinks</li>
    <li>
      <a href="https://metacpan.org/release/MAUKE/Switch-Plain-0.0501/view/lib/Switch/Plain.pm">This version</a>
    </li>
    <li>
      <a href="./Switch::Plain.html">Latest version</a>
    </li>
    <li>
<div class="plussers">
<div class="nav-header">++ed by:</div>
<div>
<a class="display-all" href="https://metacpan.org/author/MARIOROY"><img src="https://www.gravatar.com/avatar/112c64e088284a78b9afbcb83c125613?d=identicon&amp;s=20" title="MARIOROY" alt="MARIOROY"></a>
<a class="display-all" href="https://metacpan.org/author/ADAMJS"><img src="https://www.gravatar.com/avatar/62c7b6e864d6509cff5839543c676614?d=identicon&amp;s=20" title="ADAMJS" alt="ADAMJS"></a>
<a class="display-all" href="https://metacpan.org/author/SRCHULO"><img src="https://www.gravatar.com/avatar/6e788aafb8d791ad29046296ee0c5e4b?d=identicon&amp;s=20" title="SRCHULO" alt="SRCHULO"></a>
<a class="display-all" href="https://metacpan.org/author/DOLMEN"><img src="https://www.gravatar.com/avatar/70d9b050bfe39350c234d710fadfcd39?d=identicon&amp;s=20" title="DOLMEN" alt="DOLMEN"></a>
<a class="display-all" href="https://metacpan.org/author/PROCH"><img src="https://www.gravatar.com/avatar/70c238ddf0e4125f8ffb361f19b00bb9?d=identicon&amp;s=20" title="PROCH" alt="PROCH"></a>
</div>
<!-- Display counts of plussers-->
<div>
    <a href="https://metacpan.org/dist/Switch-Plain/plussers">8 PAUSE users</a>
</div>
<div>
    9 non-PAUSE users
</div>
</div>
    </li>
    <li>
<div id="metacpan_contributors">
  <div>
    <button class="btn-link"
      onclick="$(this).hide(); $('#metacpan_contributors ul').slideDown(); return false;"
    >and 1 contributors</button>
  </div>
  <ul style="display: none">
    <li class="contributor"
      data-contrib-email="l.mai@web.de"
    >
      <img class="gravatar" width="20" height="20" src="https://www.gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e?d=identicon&amp;s=20" />
      Lukas Mai
    </li>
  </ul>
</div>
    </li>
              </ul>
            </div>
          </nav>
          <div class="content-navigation">
<div class="breadcrumbs">
  <span>
    <a data-keyboard-shortcut="g a" rel="author" href="https://metacpan.org/author/MAUKE" class="author-name">Lukas Mai</a>
  </span>
  <span>&nbsp;/&nbsp;</span>
  <div class="release dist-release status-latest maturity-released">
    <span class="dropdown"><b class="caret"></b></span>
<select onchange="document.location.href=&#39;/release/&#39;+this.value+&#39;/view/lib/Switch/Plain.pm&#39;" class="">
<option
  selected
  value="MAUKE/Switch-Plain-0.0501"
>0.0501
  (MAUKE on 2017-04-16)</option>
<option
  
  value="MAUKE/Switch-Plain-0.05"
>0.05
  (MAUKE on 2016-06-10)</option>
<optgroup label="BackPAN">'
<option
  
  value="MAUKE/Switch-Plain-0.04"
>0.04
  (MAUKE on 2016-01-15)</option>
<option
  
  value="MAUKE/Switch-Plain-0.03_01"
>0.03_01 DEV
  (MAUKE on 2016-01-13)</option>
<option
  
  value="MAUKE/Switch-Plain-0.03"
>0.03
  (MAUKE on 2014-12-22)</option>
<option
  
  value="MAUKE/Switch-Plain-0.02"
>0.02
  (MAUKE on 2013-09-23)</option>
<option
  
  value="MAUKE/Switch-Plain-0.01"
>0.01
  (MAUKE on 2012-10-18)</option>
</optgroup>
</select>
    <a data-keyboard-shortcut="g d" class="release-name" href="https://metacpan.org/dist/Switch-Plain">Switch-Plain-0.0501</a>
  </div>
<span class="river-gauge-gauge">
  <svg width="24px"
       height="15px"
       version="1.1"
       xmlns="http://www.w3.org/2000/svg"
       xmlns:xlink="http://www.w3.org/1999/xlink">

    <g>
      <title>        River stage one &#10;
          • 2 direct dependents &#10;          • 6 total dependents
      </title>

      <rect x="0"  y="0" width="4" height="15" fill="#7ea3f2" />
      <rect x="5"  y="0" width="4" height="15" fill="#e4e2e2" />
      <rect x="10"  y="0" width="4" height="15" fill="#e4e2e2" />
      <rect x="15"  y="0" width="4" height="15" fill="#e4e2e2" />
      <rect x="20"  y="0" width="4" height="15" fill="#e4e2e2" />
    </g>
  </svg>

</span>
<div id="Switch-Plain-fav" class="logged_in">
<form action="https://metacpan.org/account/favorite/add" style="display: inline" onsubmit="return favDistribution(this)">
    <input type="hidden" name="remove" value="0">
    <input type="hidden" name="release" value="Switch-Plain-0.0501">
    <input type="hidden" name="author" value="MAUKE">
    <input type="hidden" name="distribution" value="Switch-Plain">
    <button type="submit" class="favorite highlight"><span>17</span> ++</button>
</form>
</div>
<div class="logged_out">
<a href="./Switch::Plain.html" onclick="alert('Please sign in to add favorites'); return false" class="favorite highlight">
<span>17</span> ++</a>
</div>
   / <span>Switch::Plain</span>
</div>
          </div>
          <main class="content">


<nav class="toc">
  <div class="toc-header"><strong>Contents</strong></div>
<ul>
  <li><a href="./Switch::Plain.html#NAME">NAME</a></li>
  <li><a href="./Switch::Plain.html#SYNOPSIS">SYNOPSIS</a></li>
  <li><a href="./Switch::Plain.html#DESCRIPTION">DESCRIPTION</a>
    <ul>
      <li><a href="./Switch::Plain.html#Syntax">Syntax</a></li>
      <li><a href="./Switch::Plain.html#Semantics">Semantics</a></li>
      <li><a href="./Switch::Plain.html#Differences-between-Switch::Plain-and-C&#39;s-switch">Differences between Switch::Plain and C&#39;s switch</a></li>
      <li><a href="./Switch::Plain.html#Scoping">Scoping</a></li>
    </ul>
  </li>
  <li><a href="./Switch::Plain.html#AUTHOR">AUTHOR</a></li>
  <li><a href="./Switch::Plain.html#COPYRIGHT-&amp;-LICENSE">COPYRIGHT &amp; LICENSE</a></li>
</ul></nav>
<div class="pod anchors">
<h1 id="NAME">NAME</h1>

<p>Switch::Plain - a simple switch statement for Perl</p>

<h1 id="SYNOPSIS">SYNOPSIS</h1>

<pre><code>  use Switch::Plain;
  
  # string version
  sswitch (get_me_a_string()) {
    # return value of get_me_a_string() is bound to $_ in this block
  
    case &#39;foo&#39;: {
      # runs if $_ equals &#39;foo&#39;
    }
  
    case &#39;bar&#39;: {
      # runs if $_ equals &#39;bar&#39;
    }
  
    case &#39;melonlord&#39; if $DEBUG: {
      # runs if $_ equals &#39;melonlord&#39; and $DEBUG is true
    }
  
    default if $VERBOSE &gt; 1: {
      # runs if nothing else matched so far and $VERBOSE is greater than 1
    }
  
    default: {
      # runs if nothing else matched so far
    }
  }
  
  # number version
  nswitch (get_me_a_number()) {
    # return value of get_me_a_number() is bound to $_ in this block
  
    case 1: {
      # runs if $_ equals 1
    }
  
    case 2: {
      # runs if $_ equals 2
    }
  
    case 99 if $DEBUG: {
      # runs if $_ equals 99 and $DEBUG is true
    }
  
    default if $VERBOSE &gt; 1: {
      # runs if nothing else matched so far and $VERBOSE is greater than 1
    }
  
    default: {
      # runs if nothing else matched so far
    }
  }</code></pre>

<h1 id="DESCRIPTION">DESCRIPTION</h1>

<p>This module provides (yet another) switch statement for Perl. Differences between this module and <a href="Switch.html">Switch</a> include:</p>

<ul>

<li><p>It&#39;s not a source filter. (It uses perl&#39;s <a href="https://metacpan.org/pod/perlapi#PL_keyword_plugin">pluggable keywords</a> instead.)</p>

</li>
<li><p>It generates non-horrible code. If you want to see this for yourself, run some sample code through <code>perl <a href="https://metacpan.org/pod/B::Deparse">-MO=Deparse</a></code>.</p>

</li>
<li><p>It doesn&#39;t try to be smart about matching fancy data structures; it only does simple string or numeric equality tests. This also sets it apart from perl&#39;s built in <a href="https://metacpan.org/pod/perlsyn#Switch-Statements"><code>given</code> statement</a> and <a href="https://metacpan.org/pod/perlop#Smartmatch-Operator">smartmatch operator <code>~~</code></a>.</p>

</li>
</ul>

<h2 id="Syntax">Syntax</h2>

<p>This module understands the following grammar:</p>

<pre><code>  switch_statement := switch_keyword switch_scrutinee switch_body

  switch_keyword := &#39;sswitch&#39; | &#39;nswitch&#39;

  switch_scrutinee := &#39;(&#39; EXPR &#39;)&#39;

  switch_body := &#39;{&#39; case_clause* &#39;}&#39;

  case_clause := case_pattern+ BLOCK

  case_pattern := case_keyword case_modifier? &#39;:&#39;

  case_keyword := &#39;default&#39; | &#39;case&#39; EXPR

  case_modifier := &#39;if&#39; EXPR | &#39;unless&#39; EXPR</code></pre>

<p><code>*</code>, <code>+</code>, and <code>?</code> have their usual regex meaning; <code>BLOCK</code> and <code>EXPR</code> represent standard Perl blocks and expressions, respectively.</p>

<h2 id="Semantics">Semantics</h2>

<p>The meaning of a switch statement is given by the following translation rules:</p>

<ul>

<li><p><code>sswitch (FOO) { ... }</code> and <code>nswitch (FOO) { ... }</code> turn into</p>

<pre><code>  do {
    local *_ = \FOO;
    ...
  };</code></pre>

<p>That is, they alias <a href="variables/$&lt;digits&gt;-($1,-$2,-...).html#$ARG"><code>$_</code></a> to <code>FOO</code> within the body of the switch statement.</p>

</li>
<li><p>A series of case clauses in the switch body turns into a single <a href="https://metacpan.org/pod/perlsyn#Compound-Statements"><code>if</code>/<code>elsif</code></a> chain. That is, the first clause becomes an <code>if</code>; every subsequent clause becomes an <code>elsif</code>.</p>

</li>
<li><p><code>case FOO:</code> becomes <code>if ($_ eq FOO)</code> for <code>sswitch</code> and <code>if ($_ == FOO)</code> for <code>nswitch</code>.</p>

<p><code>default:</code> becomes <code>if (1)</code>.</p>

<p><code>case FOO if BAR:</code> becomes <code>if ($_ eq FOO &amp;&amp; BAR)</code> for <code>sswitch</code> and <code>if ($_ == FOO &amp;&amp; BAR)</code> for <code>nswitch</code>.</p>

<p><code>default if BAR:</code> becomes <code>if (BAR)</code>.</p>

<p><code>... unless BAR</code> works similarly, but with the condition inverted (<code>!BAR</code>).</p>

<p>If there are multiple <code>case</code>/<code>default</code>s before a single block, their conditions are combined with <a href="https://metacpan.org/pod/perlop#C-style-Logical-Or"><code>||</code></a>.</p>

</li>
</ul>

<p>Here&#39;s an example demonstrating all combinations:</p>

<pre><code>  sswitch (SCRUTINEE) {
    case FOO0: {
      BODY0
    }

    case FOO1:
    case FOO2 if BAR1:
    case FOO3 unless BAR2:
    default if BAR3:
    default unless BAR4: {
      BODY1
    }

    default: {
      BODY2
    }
  }</code></pre>

<p>This is equivalent to:</p>

<pre><code>  do {
    # temporarily alias $_ to SCRUTINEE within this block:
    local *_ = \SCRUTINEE;

    if ($_ eq FOO0) {
      BODY0
    }
    elsif (
        $_ eq FOO1 ||
        ($_ eq FOO2 &amp;&amp; BAR1) ||
        ($_ eq FOO3 &amp;&amp; !BAR2) ||
        BAR3 ||
        !BAR4
    ) {
      BODY1
    }
    elsif (1) {
      BODY2
    }
  };</code></pre>

<h2 id="Differences-between-Switch::Plain-and-C&#39;s-switch"><a id="Differences"></a><a id="Differences-between-Switch::Plain-and-C-s-switch"></a>Differences between <code>Switch::Plain</code> and C&#39;s <code>switch</code></h2>

<ul>

<li><p>C&#39;s <code>switch</code> is limited to integer scrutinees. <code>Switch::Plain</code> supports any number or string.</p>

</li>
<li><p>C&#39;s <code>case</code> labels must be compile-time constants. <code>Switch::Plain</code> allows any expression and even additional arbitrary conditions via the <code>if</code>/<code>unless</code> case modifiers.</p>

</li>
<li><p>C&#39;s <code>case</code> labels are actual labels in that they can appear anywhere within the <code>switch</code> statement&#39;s body (even nested). With <code>Switch::Plain</code> all <code>case</code>s must be at the top level.</p>

</li>
<li><p>In C the order of the <code>case</code>s does not matter since they&#39;re all known at compile time and guaranteed to be distinct. <code>Switch::Plain</code> evaluates them in the order they&#39;re written: If you put <code>default: { ... }</code> in the middle of a <code>switch</code>, it will intercept all values, and any following <code>case</code>s will be ignored (this is like writing <code>... elsif (1) { ... } else { ... }</code>).</p>

</li>
<li><p>Since C&#39;s <code>case</code> labels are actual labels and C&#39;s <code>switch</code> is effectively a dynamic <code>goto</code>, C actually has no concept of a &quot;case clause&quot; or a &quot;case block&quot;. <code>switch</code> simply transfers control to one of the <code>case</code>s and that&#39;s it. This has the side effect of &quot;fallthrough&quot; behavior if you want to use C&#39;s <code>switch</code> to check for multiple distinct cases; that is, you must insert an explicit <code>break;</code> to leave the <code>switch</code> statement when you&#39;re done with your case.</p>

<p><code>Switch::Plain</code> has nothing of the kind. Because it turns into a single <code>if</code>/<code>elsif</code> chain and every case block is clearly delimited, execution of <code>sswitch</code>/<code>nswitch</code> stops as soon one case pattern matches. However, it is possible to attach multiple case patterns to a single block:</p>

<pre><code>  case 2:
  case 3:
  case 5: {
    ...
  }</code></pre>

<p>This trivial case works the same way as fallthrough would in C (any value of 2, 3, or 5 is accepted).</p>

</li>
<li><p>Since C&#39;s <code>break</code> refers to the innermost enclosing <code>switch</code> or loop, you can&#39;t use it in <code>switch</code> to leave a surrounding loop (you have to use <code>goto</code> instead). This particular problem would be avoidable in Perl thanks to <a href="https://metacpan.org/pod/perlsyn#Compound-Statements">loop labels</a>; however, this isn&#39;t even necessary because <code>sswitch</code>/<code>nswitch</code> work like <code>if</code>: They don&#39;t count as loops and <a href="functions/CORE.html#last"><code>last</code></a>/<a href="functions/CORE.html#next"><code>next</code></a>/<a href="functions/CORE.html#redo"><code>redo</code></a> ignore them.</p>

</li>
</ul>

<h2 id="Scoping">Scoping</h2>

<p>This module is a lexical pragma, i.e. the effects of <code>use Switch::Plain</code> (turning <code>sswitch</code> and <code>nswitch</code> into keywords) are scoped to the innermost enclosing block (or the whole file if there is no enclosing block).</p>

<p>If you are a module author who wants to wrap <code>Switch::Plain</code> from another module, simply call <code>Switch::Plain-&gt;import</code> from your own <code>import</code> method. It will affect whatever scope is currently being compiled (i.e. your caller).</p>

<h1 id="AUTHOR">AUTHOR</h1>

<p>Lukas Mai, <code>&lt;l.mai at web.de&gt;</code></p>

<h1 id="COPYRIGHT-&amp;-LICENSE"><a id="COPYRIGHT"></a><a id="COPYRIGHT---LICENSE"></a>COPYRIGHT &amp; LICENSE</h1>

<p>Copyright 2012-2013 Lukas Mai.</p>

<p>This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.</p>

<p>See http://dev.perl.org/licenses/ for more information.</p></div>

<div id="metacpan_install-instructions-dialog" class="modal fade">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title">Module Install Instructions</h4>
      </div>
      <div class="modal-body">
        <p>To install Switch::Plain, copy and paste the appropriate command in to your terminal.</p>
        <p><a href="https://metacpan.org/dist/App-cpanminus/view/bin/cpanm">cpanm</a></p>
        <pre><code>cpanm Switch::Plain</code></pre>
        <p><a href="https://metacpan.org/pod/CPAN">CPAN shell</a></p>
        <pre><code>perl -MCPAN -e shell
install Switch::Plain</code></pre>
        <p>For more information on module installation, please visit <a href="https://www.cpan.org/modules/INSTALL.html">the detailed CPAN module installation guide</a>.</p>
      </div>
      <div class="modal-footer">
        <a href="./Switch::Plain.html#" data-dismiss="modal" class="btn">Close</a>
      </div>
    </div>
  </div>
</div>
          </main>
          <div class="content-pagination">
          </div>
        </div>
        <footer class="footer">
          <div class="footer-container">
            <div class="footer-social">
              <div class="footer-link footer-logo">
                <a href="https://metacpan.org/">
                  <img src="https://metacpan.org/static/images/metacpan-logo.svg" alt="MetaCPAN" />
                </a>
              </div>
              <a class="footer-social-link" href="https://github.com/metacpan">
                <i class="fab fa-github-square"></i>
              </a>
              <a class="footer-social-link" href="https://fosstodon.org/@metacpan">
                <i class="fab fa-mastodon"></i>
              </a>
            </div>
            <div class="footer-links">
              <div class="footer-link">
                  <a href="https://metacpan.org/about">About</a>
              </div>
              <div class="footer-link">
                  <a href="https://metacpan.org/about/sponsors">Sponsor</a>
              </div>
              <div class="footer-link">
                  <a href="https://grep.metacpan.org">grep::cpan</a>
              </div>
              <div class="footer-link">
                  <a href="https://metacpan.org/recent">Recent</a>
              </div>
              <div class="footer-link">
                  <a href="https://metacpan.org/about/faq">FAQ</a>
              </div>
              <div class="footer-link">
                  <a href="https://metacpan.org/tools">Tools</a>
              </div>
              <div class="footer-link">
                  <a href="https://fastapi.metacpan.org/">API</a>
              </div>
              <div class="footer-link">
                  <a href="https://www.perl.org/">Perl.org</a>
              </div>
            </div>
            <div class="footer-sponsors">
              <a class="footer-sponsor-link" target="_blank" href="https://www.bytemark.co.uk/" rel="noopener">
                <img class="footer-sponsor-bytemark" src="https://metacpan.org/static/images/sponsors/bytemark_logo.svg" alt="Bytemark logo">
              </a>
              <a class="footer-sponsor-link" target="_blank" href="https://www.liquidweb.com/" rel="noopener">
                <img class="footer-sponsor-liquidweb" src="https://metacpan.org/static/images/sponsors/liquidweb_logo.png" alt="liquidweb logo">
              </a>
              <a class="footer-sponsor-link" target="_blank" href="https://deriv.com/careers/" rel="noopener">
                <img class="footer-sponsor-deriv" src="https://metacpan.org/static/images/sponsors/deriv.svg" alt="Deriv logo">
              </a>
              <a class="footer-sponsor-link" target="_blank" href="https://geocode.xyz" rel="noopener">
                <img class="footer-sponsor-geocode" src="https://metacpan.org/static/images/sponsors/geocodelogo.svg" alt="Geocode logo">
              </a>
              <a class="footer-sponsor-link" target="_blank" href="https://www.fastly.com/" rel="noopener">
                <img class="footer-sponsor-fastly" src="https://metacpan.org/static/images/sponsors/fastly_logo.svg" alt="Fastly logo">
              </a>
              <a class="footer-sponsor-link" target="_blank" href="https://opencagedata.com" rel="noopener">
                <img class="footer-sponsor-opencage" src="https://metacpan.org/static/images/sponsors/open-cage.svg" alt="OpenCage logo">
              </a>
            </div>
          </div>
        </footer>
        <div class="modal fade" tabindex="-1" role="dialog" id="metacpan_keyboard-shortcuts">
          <div class="modal-dialog">
            <div class="modal-content">
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Keyboard Shortcuts</h4>
              </div>
              <div class="modal-body row">
<div class="col-md-6">
  <table class="table keyboard-shortcuts">
    <thead>
      <tr>
        <th></th>
        <th>Global</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td class="keys">
          <kbd>s</kbd>
        </td>
        <td>Focus search bar</td>
      </tr>
      <tr>
        <td class="keys">
          <kbd>?</kbd>
        </td>
        <td>Bring up this help dialog</td>
      </tr>
    </tbody>
  </table>

  <table class="table keyboard-shortcuts">
    <thead>
      <tr>
        <th></th>
        <th>GitHub</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td class="keys">
          <kbd>g</kbd> <kbd>p</kbd>
        </td>
        <td>Go to pull requests</td>
      </tr>
      <tr>
        <td class="keys">
          <kbd>g</kbd> <kbd>i</kbd>
        </td>
        <td>go to github issues (only if github is preferred repository)</td>
      </tr>
    </tbody>
  </table>
</div>

<div class="col-md-6">
  <table class="table keyboard-shortcuts">
    <thead>
      <tr>
        <th></th>
        <th>POD</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td class="keys">
          <kbd>g</kbd> <kbd>a</kbd>
        </td>
        <td>Go to author</td>
      </tr>
      <tr>
        <td class="keys">
          <kbd>g</kbd> <kbd>c</kbd>
        </td>
        <td>Go to changes</td>
      </tr>
      <tr>
        <td class="keys">
          <kbd>g</kbd> <kbd>i</kbd>
        </td>
        <td>Go to issues</td>
      </tr>
      <tr>
        <td class="keys">
          <kbd>g</kbd> <kbd>d</kbd>
        </td>
        <td>Go to dist</td>
      </tr>
      <tr>
        <td class="keys">
          <kbd>g</kbd> <kbd>r</kbd>
        </td>
        <td>Go to repository/SCM</td>
      </tr>
      <tr>
        <td class="keys">
          <kbd>g</kbd> <kbd>s</kbd>
        </td>
        <td>Go to source</td>
      </tr>
      <tr>
        <td class="keys">
          <kbd>g</kbd> <kbd>b</kbd>
        </td>
        <td>Go to file browse</td>
      </tr>

    </tbody>
  </table>
</div>

<div class="col-md-12">
  <table class="table keyboard-shortcuts">
    <thead>
      <tr>
        <th></th>
        <th>Search terms</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td><em>module:</em> (e.g. <a href="https://metacpan.org/search?q=module%3APlugin">module:Plugin</a>)</td>
      </tr>
      <tr>
        <td><em>distribution:</em> (e.g. <a href="https://metacpan.org/search?q=distribution%3ADancer+auth">distribution:Dancer auth</a>)</td>
      </tr>
      <tr>
        <td><em>author:</em> (e.g. <a href="https://metacpan.org/search?q=author%3ASONGMU+Redis">author:SONGMU Redis</a>)</td>
      </tr>
      <tr>
        <td><em>version:</em> (e.g. <a href="https://metacpan.org/search?q=version%3A1.00">version:1.00</a>)</td>
      </tr>
    </tbody>
  </table>
</div>
              </div>
              <div class="modal-footer"></div>
            </div>
          </div>
        </div>
    </body>
</html>
